﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Diagnostics;
using System.Threading;

namespace AutoUpdate
{
    public partial class FormAo : Telerik.WinControls.UI.RadForm
    {
        public FormAo()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;

            backgroundWorker1 = new BackgroundWorker() { WorkerReportsProgress = true, WorkerSupportsCancellation = true };

            backgroundWorker1.DoWork += backgroundWorker1_DoWork;
            backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
            backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
        }
        public void chayexe(string exepatch)
        {

            //Application.StartupPath + @"\AutoUpdate.EXE";
            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = exepatch };

                Process.Start(startInfo);
            }
            catch (Exception ex)
            {
                RadMessageBox.Show("Lỗi " + ex.ToString(), "Cảnh Báo", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
        }
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (!backgroundWorker1.CancellationPending)
            {
                //     radLabel1.Text = "Hoàn Thành Được " + e.ProgressPercentage + "% Tiến Trình Update Chương Trình Từ Version ";
                radLabel1.Text = String.Format("Hoàn Thành Được {0}% Tiến Trình Update Chương Trình Từ Version ", e.ProgressPercentage);
                //  lblPercent.Text = e.ProgressPercentage + "%";
                //  radProgressBar1.PerformStep();

            }


        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            //RadMessageBox.Show("Tiến trình cập nhật thành công !!! ", "Thông Báo", MessageBoxButtons.OK, RadMessageIcon.Info);
            this.Close();

            chayexe(Application.StartupPath + @"\Bitis_KeToanThue_Pro_Update.EXE");


        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DoSomething();
        }
        private void DoSomething()
        {


            /* try
             {
                 ch.CopyFolder(frmLogin.duongdancauhinh, Application.StartupPath);
                 ketqua = 1;

             }
             catch(Exception ex)
             {
                 ketqua = 0;
                 RadMessageBox.Show("Lỗi :"+ex.ToString(), "Thông Báo", MessageBoxButtons.OK, RadMessageIcon.Error);
             }*/
            while (radProgressBar1.Value1 < radProgressBar1.Maximum)
            {
                if (backgroundWorker1.CancellationPending)
                {

                    break;


                }
                //báo cáo tiến độ của tiến trình


                radProgressBar1.Value1 = radProgressBar1.Value1 + 10;
                backgroundWorker1.ReportProgress(radProgressBar1.Value1);
                Thread.Sleep(5);

            }


            // Thread.Sleep(20000);
        }

        private void FormAo_Load(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

    }
}
